package homework7.soso.service.impl;

import homework7.soso.entity.MobileCard;
import homework7.soso.service.CallService;
import homework7.soso.service.SendService;

/**
 * @author fengq
 * 话痨套餐
 */
public class TalkPackage extends ServicePackage implements SendService, CallService {
    /**
     * 通话时间
     */
    private int talkTime;
    /**
     * 短信条数
     */
    private int smsCount;

    @Override
    public void showInfo() {
        System.out.println("话痨套餐:通话时长为500分钟/月，短信条数为30条/ 月");
        }

public TalkPackage(double price, int talkTime, int smsCount) {
        this.price=price;
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        }

public TalkPackage(int talkTime, int smsCount) {
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        }

public int getTalkTime() {
        return talkTime;
        }

public void setTalkTime(int talkTime) {
        this.talkTime = talkTime;
        }

public int getSmsCount() {
        return smsCount;
        }

public void setSmsCount(int smsCount) {
        this.smsCount = smsCount;
        }

@Override
public int call(int minCount, MobileCard mobileCard) throws Exception {
        TalkPackage talkPackage = (TalkPackage) mobileCard.getServicePackage();
        int time = minCount;
        for (int i = 0; i < minCount; i++) {
        //套餐剩余通话时长大于一分钟
        if (talkPackage.getTalkTime()-mobileCard.getRealTalkTime()>0) {

        mobileCard.setRealTalkTime(mobileCard.getRealTalkTime()+1);
        }else if (mobileCard.getMoney()>=0.2) {
        //账户余额大于0.2yuan

        mobileCard.setMoney(mobileCard.getMoney()-0.2);
        mobileCard.setConsumeAmount(mobileCard.getConsumeAmount()+0.2);
        }else {
        time = i;
        //因为抛出异常后，下面的语句不会执行，而返回值为零。所以可以通过setMongey暂存。
        mobileCard.setMoney(time);
        throw new Exception("本次通话"+time+"分钟，您的余额不足，请充值后再使用");
        }
        }
        return time;

        }

@Override
public int send(int count, MobileCard card) throws Exception {
        TalkPackage talkPackage = (TalkPackage) card.getServicePackage();
        int time = count;
        for (int i = 0; i < count; i++) {
        if (talkPackage.getSmsCount()-card.getRealSMSCount()>0) {

        card.setRealSMSCount(card.getRealSMSCount()+1);
        }else if (card.getMoney()>=0.1) {

        card.setMoney(card.getMoney()-0.1);
        card.setConsumeAmount(card.getConsumeAmount()+0.1);
        }else {
        time = i;
        card.setMoney(time);
        throw new Exception("本次发信息"+time+"条，您的余额不足，请充值后使用");
        }
        }
        return time;
        }
        }
